import mc
import xbmc
import urllib
import sage
import config
from sage import SageApi, SageApiException

config.initialize()

# Make sure server, jetty user name and jetty password are non-empty.
server = config.getSageServer()

while len(server)==0:
	server = mc.ShowDialogKeyboard("Enter Sage server name:port. Eg 192.168.1.121", "", False)
	config.setSageServer(server)

jettyname = config.getJettyUser()

while len(jettyname)==0:
	jettyname = mc.ShowDialogKeyboard("Enter user name:", "", False)
	config.setJettyUser(jettyname)


jettypassword = config.getJettyPassword()

while len(jettypassword)==0:
	jettypassword = mc.ShowDialogKeyboard("Enter password:", "", True)
	config.setJettyPassword(jettypassword)

# Create the global sageAPI object and check that the server is alive.
sageAPI = SageApi(server, jettyname, jettypassword)

try:
	sageAPI.isApiServerAlive()
except SageApiException, e:
	startAnyway = mc.ShowDialogConfirm("The server could not be reached.", server + " could not be reached using the given user name and password. [CR][CR]Do you want to continue anyway?", "No", "Yes")
	print "RESPONSE="+str(startAnyway)
	if not startAnyway:
		exit(1)

# Global dictionary used by Recordings menu to store grouped shows.
gRecordingsGroups = {}

# Global var used by Recordings menu to keep track of what show is focused in left pane.
gRecordingsFocusedShow = 0

mc.LogDebug("SageBoxee: Launching main window.")
mc.ActivateWindow(14000)

# Launch status icon updater thread.
#
# t.getStatus() returns the current status of the icons.
# t.setKeepAlive(False) stops the thread.
# t.setActiveWindow(int) tells the thread which window to update.
statusUpdater = sage.IconStatusThread(sageAPI)
statusUpdater.start()
statusUpdater.setActiveWindow(14000)